package com.atguigu.gmall.payment.service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.task.config.MqConst;
import com.atguigu.gmall.task.service.RabbitService;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author: 李旭
 * @create: 2021-07-26 15:36
 **/
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderFeignClient orderFeignClient;
    //生成支付宝
    @Override
    public PaymentInfo insert(Long orderId, PaymentType type) {

        //幂等性问题：防止重复性保存支付表  一张订单对应一个支付表
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(
                new QueryWrapper<PaymentInfo>().eq("order_id", orderId));
        if(null == paymentInfo){
            //创建
            paymentInfo = new PaymentInfo();
            OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            paymentInfo.setOrderId(orderInfo.getId());
            //交易编号 （无）
            paymentInfo.setPaymentType(type.name());
            paymentInfo.setSubject(orderInfo.getTradeBody());
            paymentInfo.setCreateTime(new Date());
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            //回调时间
            //回调内容
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());

            paymentInfoMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }


    @Autowired
    private RabbitService rabbitService;
    //更新支付表
    @Override
    public void updatePayment(Map<String, String> paramsMap) {

        //幂等性问题 ：防止 重复更新
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("out_trade_no", paramsMap.get("out_trade_no")));

        if(PaymentStatus.UNPAID.name().equals(paymentInfo.getPaymentStatus())){
            //是支付中
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            //银行流水号
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            //回调时间
            paymentInfo.setCallbackTime(new Date());
            //保存回调的内容
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
            //更新支付表
            paymentInfoMapper.updateById(paymentInfo);
            //发送ＭＱ　
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());
        }



    }
}
